﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace OnlineStore
{
    public interface IEntitiesContext
    {
        IDbSet<Product> Products { get; set; }
        IDbSet<Category> Categorys { get; set; }
        IDbSet<Brand> Brands { get; set; }
        IDbSet<Cart> Carts { get; set; }
        IDbSet<Order> Orders { get; set; }
        IDbSet<OrderDetail> OrderDetails { get; set; }
        int SaveChanges();
        DbSet<T> Set<T>() where T : class;
    }

    public class EntitiesContext : DbContext, IEntitiesContext
    {
        public EntitiesContext()
            : base("OnlineStore")
        {
        }

        public IDbSet<Product> Products { get; set; }
        public IDbSet<Category> Categorys { get; set; }
        public IDbSet<Brand> Brands { get; set; }
        public IDbSet<Cart> Carts { get; set; }
        public IDbSet<Order> Orders { get; set; }
        public IDbSet<OrderDetail> OrderDetails { get; set; }

        //public DbSet<UserProfile> UserProfiles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<Membership>()
            //    .HasMany<Role>(r => r.Roles)
            //    .WithMany(u => u.Members)
            //    .Map(m =>
            //    {
            //        m.ToTable("webpages_UsersInRoles");
            //        m.MapLeftKey("UserId");
            //        m.MapRightKey("RoleId");
            //    });

            modelBuilder.Entity<Product>().Property(p => p.ListPrice).HasPrecision(12, 0);
            modelBuilder.Entity<Product>().Property(p => p.DealPrice).HasPrecision(12, 0);
        }

    }
}